// TypeScript file
module Fighter {
    /**摇杆方法 */
    export class Joystick extends eui.Component {

        constructor(gan: eui.Image, huan: eui.Image) {
            super();
            this.addEventListener(egret.Event.ENTER_FRAME, this.PlayerMove, this);
            this.Gan = gan;
            this.Huan = huan;
            this.old_position.x = this.Gan.x;
            this.old_position.y = this.Gan.y;
        }
        /**背景 */
        private Bg:eui.Image;
        /**杆*/
        private Gan: eui.Image;
        /**环*/
        private Huan: eui.Image;
        /**触点相对位置*/
        public touchDistance: egret.Point = new egret.Point;
        /**触摸状态*/
        private touchStatus: boolean = false;
        /**飞机飞行方向，用point类型代替向量 */
        public direction: egret.Point = new egret.Point();
        private old_position: egret.Point = new egret.Point();
        /**玩家移动方向*/
        public PlayerMove() {
            GameUtil.PlayerMessage.instance.joystick_direction=this.direction;
            //console.log(PlayerMsg.instance.joystick_direction.x);
            
        }
        /**游戏摇杆*/
        public Game_joystick(gan: eui.Image, huan: eui.Image): void {
            this.Gan.touchEnabled = true;
            this.Gan.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.mouseDown, this);
            this.Gan.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.mouseUp, this);
            this.Gan.addEventListener(egret.TouchEvent.TOUCH_END, this.mouseUp, this);
        }
        //触摸摇杆的三个相关方法
        public mouseDown(evt: egret.TouchEvent) {
            this.touchStatus = true;
            this.touchDistance.x = evt.stageX - this.Gan.x;
            this.touchDistance.y = evt.stageY - this.Gan.y;
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.mouseMove, this);
        }
        //拖动
        private mouseMove(evt: egret.TouchEvent) {
            let new_position = new egret.Point(evt.stageX - this.touchDistance.x, evt.stageY - this.touchDistance.y);
            if (egret.Point.distance(this.old_position, new_position) < this.Huan.height / 2) {
                this.Gan.x = evt.stageX - this.touchDistance.x;
                this.Gan.y = evt.stageY - this.touchDistance.y;
            }
            if (egret.Point.distance(this.old_position, new_position) >= this.Huan.height / 2) 
            {
                
                this.Gan.x = (this.Huan.height / 2) / egret.Point.distance(this.old_position, new_position) * (new_position.x - this.old_position.x) + this.old_position.x;
                this.Gan.y = (this.Huan.height / 2) / egret.Point.distance(this.old_position, new_position) * (new_position.y - this.old_position.y) + this.old_position.y;
            }
            let vector2 =
                new egret.Point(new_position.x - this.old_position.x, new_position.y - this.old_position.y);
            let sqrt_vector2 = Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y);
            this.direction.x = vector2.x / sqrt_vector2;
            this.direction.y = vector2.y / sqrt_vector2;
        }
        public mouseUp(event: egret.Event) {
            this.touchStatus = false;
            this.Gan.x = this.old_position.x;
            this.Gan.y = this.old_position.y;
            if(this.$hasAddToStage)
            {
                this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.mouseMove, this);                
            }
            this.direction.x = 0;
            this.direction.y = 0;

        }
    }
}